package com.newly.center.business.api;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newly.api.business.feign.MQTTDeviceClient;
import com.newly.center.business.service.MQTTDeviceService;
import com.newly.common.base.annotation.NewlyLog;
import com.newly.common.base.constants.CodeConstant;
import com.newly.common.base.constants.MenuConstant;
import com.newly.common.base.constants.ServerUriConstant;
import com.newly.common.base.entity.R;
import com.newly.common.base.entity.business.Vo.MqttDeviceVo;
import com.newly.common.base.entity.device.po.DevicePo;
import com.newly.common.base.entity.device.vo.ExcelDevice;
import com.newly.common.base.exception.NewlyException;
import com.newly.common.core.utils.ExcelUtil;
import com.newly.common.core.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(ServerUriConstant.NEWLY_BUSINESS_MQTT_Device)
public class MQTTDeviceController implements MQTTDeviceClient {

    @Autowired
    MQTTDeviceService MQTTDeviceService;

    @Autowired
    private RequestUtils requestUtils;

    @NewlyLog(ability = MenuConstant.Ability.QUE, title = "直播流", isOpen = false)
    @Override
    public R getDeviceFlvUrl(String id, String type) {
        return MQTTDeviceService.getDeviceFlvUrl(id, type);
    }

    @Override
    public R getDateOfExistenceOfMedia(String siteId, String startDate, String endDate) {

        return R.ok(MQTTDeviceService.getDateOfExistenceOfMedia(siteId, startDate, endDate));
    }

    @NewlyLog(ability = MenuConstant.Ability.QUE, title = "回放流", isOpen = false)
    @Override
    public R getDeviceHistoryFlvUrl(String date, String siteId) {

        return R.ok(this.MQTTDeviceService.getDeviceHistoryFlvUrl(date, siteId));
    }

    @Override
    public R getLivingGateway() {
        return R.ok(MQTTDeviceService.getLivingGateway());
    }

    @Override
    public R search(MqttDeviceVo data) {
        return MQTTDeviceService.search(data);
    }

    @NewlyLog(ability = MenuConstant.Ability.ADD, title = "设备", isOpen = true)
    @Override
    public R add(MqttDeviceVo data) {
        return MQTTDeviceService.add(data);
    }

    @NewlyLog(ability = MenuConstant.Ability.DEL, title = "设备", isOpen = true)
    @Override
    public R delete(List<String> ids) {
        return MQTTDeviceService.delete(ids);
    }

    @NewlyLog(ability = MenuConstant.Ability.DEL, title = "槽", isOpen = true)
    @Override
    public R deleteCamera(MqttDeviceVo data, int slot, int[] channelIndex) {
        return MQTTDeviceService.deleteCamera(data, slot, channelIndex);
    }

    @NewlyLog(ability = MenuConstant.Ability.UPD, title = "设备", isOpen = true)
    @Override
    public R modify(MqttDeviceVo data) {
        return MQTTDeviceService.modify(data);
    }

    @NewlyLog(ability = MenuConstant.Ability.UPD, title = "网关", isOpen = true)
    @Override
    public R modifyGateway(MqttDeviceVo data) {
        return MQTTDeviceService.modifyGateway(data);
    }

    @NewlyLog(ability = MenuConstant.Ability.DEL, title = "网关", isOpen = true)
    @Override
    public R deleteGateway(List<String> ids) {
        return MQTTDeviceService.deleteGateway(ids);
    }

    @Override
    public R modifyVideoId(MqttDeviceVo data) {
        return MQTTDeviceService.modifyVideoId(data);
    }

    @Override
    public R getList(MqttDeviceVo data, int[] slot) {
        return MQTTDeviceService.getList(data, slot);
    }

    @Override
    public R deviceSync(MqttDeviceVo data) {
        return MQTTDeviceService.deviceSync(data);
    }

    @Override
    public void export(HttpServletResponse response, String keyword, Integer deviceStatus, Long siteId, Integer type) {
        Page<DevicePo> page = (Page<DevicePo>) MQTTDeviceService
                .getDeviceList(keyword, deviceStatus, siteId, type, 1, 1).getData();
        //获取总数据量
        int total = Math.toIntExact(page.getTotal());
        if (total < 1) {
            throw new NewlyException(CodeConstant.FAILED, "无数据");
        }
        Page<DevicePo> data = (Page<DevicePo>) MQTTDeviceService
                .getDeviceList(keyword, deviceStatus, siteId, type, 1, total).getData();
        List<ExcelDevice> collect = data.getRecords().stream().map(po -> {
            String[] ignoreProperties = new String[]{"type", "deviceStatus", "isAvailable", "typeAlias"};
            ExcelDevice excelDevice = BeanUtil.copyProperties(po, ExcelDevice.class, ignoreProperties);
            String type1;
            switch (po.getType()) {
                case 1:
                    type1 = "网关";
                    break;
                case 2:
                    type1 = "地磅";
                    break;
                case 3:
                    type1 = "轮轴检测设备";
                    break;
                case 4:
                    type1 = "车牌抓拍设备";
                    break;
                default:
                    type1 = "";
            }
            String deviceStatus1 = po.getDeviceStatus();
            String isAvailable = po.getIsAvailable();
            String typeAlias;
            switch (po.getTypeAlias()) {
                case 0:
                    typeAlias = "无标识";
                    break;
                case 1:
                    typeAlias = "车牌识别摄像机";
                    break;
                case 2:
                    typeAlias = "轮轴识别摄像机";
                    break;
                default:
                    typeAlias = "";
            }
            excelDevice.setType(type1)
                    .setDeviceStatus("1".equals(deviceStatus1) ? "在线" : "离线")
                    .setIsAvailable("1".equals(isAvailable) ? "可用" : "不可用")
                    .setTypeAlias(typeAlias);
            return excelDevice;
        }).collect(Collectors.toList());

        ExcelUtil.export(response, "设备管理表", null, collect, ExcelDevice.class);
    }

    @Override
    public R getDeviceList(String keyword, Integer deviceStatus, Long siteId, Integer type, Integer pageNum, Integer pageSize) {
        return MQTTDeviceService.getDeviceList(keyword, deviceStatus, siteId, type, pageNum, pageSize);
    }

    @Override
    public R getDeviceTree(String keyword) {
        return MQTTDeviceService.getDeviceTree(keyword);
    }

    @Override
    public R queryByIdsInAuth(List<String> deviceIds) {
        List<DevicePo> devicePos = null;
        try {
            devicePos = MQTTDeviceService.getByIdsInAuth(deviceIds);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(devicePos);
    }
}
